#include "cataloguelistmodel.h"

#include <QVariant>
#include <QDebug>

CatalogueListModel::CatalogueListModel(QObject* parent/* = nullptr*/) : QAbstractListModel(parent)
{
}

QVariant CatalogueListModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid()) return QVariant();
    switch (role) {
    case Qt::DisplayRole:
        return _dataList.at(index.row()).content;
    default:
        return QVariant();
    }
    return QVariant();
}

int CatalogueListModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return _dataList.count();
}

void CatalogueListModel::setDataList(const QList<CatalogueItem>& list)
{
    if(!_dataList.isEmpty()) {
        beginRemoveRows(QModelIndex(), 0, _dataList.count() - 1);
        _dataList.clear();
        endRemoveRows();
    }
    if(list.isEmpty()) return;
    beginInsertRows(QModelIndex(), 0, list.count() - 1);
    _dataList = list;
    endInsertRows();
}

CatalogueItem CatalogueListModel::item(int row)
{
    if(0 > row || row >= _dataList.count() || _dataList.isEmpty()) return CatalogueItem();
    return _dataList.at(row);
}
